package com.bh.web.controller.extend;

import com.bh.common.annotation.Log;
import com.bh.common.core.controller.BaseController;
import com.bh.common.core.domain.AjaxResult;
import com.bh.common.core.page.TableDataInfo;
import com.bh.common.enums.BusinessType;
import com.bh.common.utils.poi.ExcelUtil;
import com.bh.extend.domain.JcCustomerPrice;
import com.bh.extend.service.IJcCustomerPriceService;
import com.bh.extend.vo.JcCustomerPriceVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 客户单价Controller
 *
 * @author slx
 * @date 2023-06-15
 */
@RestController
@RequestMapping("/extend/customerPrice")
public class JcCustomerPriceController extends BaseController {
    @Autowired
    private IJcCustomerPriceService jcCustomerPriceService;

    /**
     * 查询客户单价列表
     */
    @ApiOperation("查询客户单价列表")
    @PreAuthorize("@ss.hasPermi('extend:customerPrice:list')")
    @GetMapping("/list")
    public TableDataInfo list(JcCustomerPrice jcCustomerPrice) {
        startPage();
        List<JcCustomerPrice> list = jcCustomerPriceService.selectJcCustomerPriceList(jcCustomerPrice);
        return getDataTable(list);
    }

    /**
     * 导出客户单价列表
     */
    @PreAuthorize("@ss.hasPermi('extend:customerPrice:export')")
    @Log(title = "客户单价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JcCustomerPrice jcCustomerPrice) {
        List<JcCustomerPriceVo> list = jcCustomerPriceService.selectJcCustomerPriceVoList(jcCustomerPrice);
        ExcelUtil<JcCustomerPriceVo> util = new ExcelUtil<>(JcCustomerPriceVo.class);
        util.exportExcel(response, list, "客户单价数据");
    }


    @Log(title = "用户管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('extend:customerPrice:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<JcCustomerPriceVo> util = new ExcelUtil<>(JcCustomerPriceVo.class);
        List<JcCustomerPriceVo> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = jcCustomerPriceService.importData(userList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<JcCustomerPriceVo> util = new ExcelUtil<>(JcCustomerPriceVo.class);
        util.importTemplateExcel(response, "客户单价数据");
    }

    /**
     * 获取客户单价详细信息
     */
    @ApiOperation("获取客户单价详细信息")
    @PreAuthorize("@ss.hasPermi('extend:customerPrice:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(jcCustomerPriceService.selectJcCustomerPriceById(id));
    }

    /**
     * 新增客户单价
     */
    @ApiOperation("新增客户单价")
    @PreAuthorize("@ss.hasPermi('extend:customerPrice:add')")
    @Log(title = "客户单价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody JcCustomerPrice jcCustomerPrice) {
        return toAjax(jcCustomerPriceService.insertJcCustomerPrice(jcCustomerPrice));
    }

    /**
     * 修改客户单价
     */
    @ApiOperation("修改客户单价")
    @PreAuthorize("@ss.hasPermi('extend:customerPrice:edit')")
    @Log(title = "客户单价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody JcCustomerPrice jcCustomerPrice) {
        return toAjax(jcCustomerPriceService.updateJcCustomerPrice(jcCustomerPrice));
    }

    /**
     * 删除客户单价
     */
    @ApiOperation("删除客户单价")
    @PreAuthorize("@ss.hasPermi('extend:customerPrice:remove')")
    @Log(title = "客户单价", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(jcCustomerPriceService.deleteJcCustomerPriceByIds(ids));
    }
}
